<?php
class Pageley_Imaging_Brush_Color implements Pageley_Imaging_Brush {
    public static $BLACK;
    public static $GREEN;
    public static $RED;
    public static $BLUE;
    public static $WHITE;
    public static $DARK_GREEN;
    public static $DARK_RED;
    public static $DARK_BLUE;
    public static $GRAY;
    public static $YELLOW;
    public static $MAGENTA;
    public static $CYAN;
    public static $DARK_GRAY;
    public static $OLIVE;
    public static $PURPLE;
    public static $TEAL;
    public static $TRANSPARENT;
    
    private $red;
    private $green;
    private $blue;
    private $alpha;
    
    public function __construct($red, $green, $blue, $alpha = 0xFF) {
        $this->red = $red;
        $this->green = $green;
        $this->blue = $blue;
        $this->alpha = $alpha;
    }
    
    public static function fromHex($hex) {
        $matches = preg_match("/^#?([0-9a-fA-F]{2})?[0-9a-fA-F]{6}$/", $hex);
        if($matches == 0) {
            throw new Pageley_Util_IllegalArgumentException("Illegal hex color string: " . $hex);
        }
        if(Pageley_Util_String::startsWith($hex, "#")) {
            $hex = Pageley_Util_String::substring($hex, 1);
        }
        $alpha = 0xFF;
        if(Pageley_Util_String::length($hex) == 8) {
            $alpha = hexdec(Pageley_Util_String::substring($hex, 0, 2));
            $hex = Pageley_Util_String::substring($hex, 2);
        }
        $red = hexdec(Pageley_Util_String::substring($hex, 0, 2));
        $green = hexdec(Pageley_Util_String::substring($hex, 2, 2));
        $blue = hexdec(Pageley_Util_String::substring($hex, 4));
        return new Pageley_Imaging_Brush_Color($red, $green, $blue, $alpha);
    }
    
    public static function fromColorId($colorId) {
        $opacity = (int) ($colorId / 0x1000000);
        $red = ((int) ($colorId / 0x10000)) % 0x100;
        $green = ((int) ($colorId / 0x100)) % 0x100;
        $blue = $colorId % 0x100;
        return new Pageley_Imaging_Brush_Color($red, $green, $blue, Pageley_Imaging_Brush_Color::opacityToAlpha($opacity));
    }
    
    private static function opacityToAlpha($opacity) {
        $alpha = (0x7F - $opacity) * 2;
        if($alpha == 254) {
            $alpha = 255;
        }
        return $alpha;
    }
    
    private static function alphaToOpacity($alpha) {
        return (int) ((0xFF - $alpha) / 2);
    }
    
    public function getColorId($resource) {
        $opacity = Pageley_Imaging_Brush_Color::alphaToOpacity($this->alpha);
        return imagecolorallocatealpha($resource, $this->red, $this->green, $this->blue, $opacity);
    }
    
    public function __toString() {
        return "color(" . $this->red . ", " . $this->green . ", " . $this->blue . ", " . $this->alpha . ")";
    }
    
    public function equals($o) {
        if($o == null || !($o instanceof Pageley_Imaging_Brush_Color)) {
            return false;
        }
        return $this->red == $o->red && 
            $this->green == $o->green && 
            $this->blue == $o->blue && 
            $this->alpha == $o->alpha;
    }
}

Pageley_Imaging_Brush_Color::$TRANSPARENT = new Pageley_Imaging_Brush_Color(0x00, 0x00, 0x00, 0x00);
Pageley_Imaging_Brush_Color::$BLACK = new Pageley_Imaging_Brush_Color(0x00, 0x00, 0x00);
Pageley_Imaging_Brush_Color::$GREEN = new Pageley_Imaging_Brush_Color(0x00, 0xFF, 0x00);
Pageley_Imaging_Brush_Color::$RED = new Pageley_Imaging_Brush_Color(0xFF, 0x00, 0x00);
Pageley_Imaging_Brush_Color::$BLUE = new Pageley_Imaging_Brush_Color(0x00, 0x00, 0xFF);
Pageley_Imaging_Brush_Color::$WHITE = new Pageley_Imaging_Brush_Color(0xFF, 0xFF, 0xFF);
Pageley_Imaging_Brush_Color::$DARK_GREEN = new Pageley_Imaging_Brush_Color(0x00, 0x80, 0x00);
Pageley_Imaging_Brush_Color::$DARK_RED = new Pageley_Imaging_Brush_Color(0x80, 0x00, 0x00);
Pageley_Imaging_Brush_Color::$DARK_BLUE = new Pageley_Imaging_Brush_Color(0x00, 0x00, 0x80);
Pageley_Imaging_Brush_Color::$GRAY = new Pageley_Imaging_Brush_Color(0xC0, 0xC0, 0xC0);
Pageley_Imaging_Brush_Color::$YELLOW = new Pageley_Imaging_Brush_Color(0xFF, 0xFF, 0x00);
Pageley_Imaging_Brush_Color::$MAGENTA = new Pageley_Imaging_Brush_Color(0xFF, 0x00, 0xFF);
Pageley_Imaging_Brush_Color::$CYAN = new Pageley_Imaging_Brush_Color(0x00, 0xFF, 0xFF);
Pageley_Imaging_Brush_Color::$DARK_GRAY = new Pageley_Imaging_Brush_Color(0x80, 0x80, 0x80);
Pageley_Imaging_Brush_Color::$OLIVE = new Pageley_Imaging_Brush_Color(0x80, 0x80, 0x00);
Pageley_Imaging_Brush_Color::$PURPLE = new Pageley_Imaging_Brush_Color(0x80, 0x00, 0x80);
Pageley_Imaging_Brush_Color::$TEAL = new Pageley_Imaging_Brush_Color(0x00, 0x80, 0x80);
